package edu.msu.scrabble.project1;

import android.os.Bundle;
import android.os.Handler;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class WaitingActivity extends GlobalMenuActivity {

	private static String TEXT = "SAVETEXT";
	
	private TextView view;
	private boolean donePoll = false;
	private boolean cancel = false;

	private Thread thread;
	
	//private Bitmap theImage = null;
	
	
	/**
	 * 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_waiting);
		thecloud = Cloud.get();
		view = (TextView)findViewById(R.id.textView1);
		
		
		if(savedInstanceState != null){
			String text = savedInstanceState.getString(TEXT);
			view.setText(text);
		}
		cancel = false;
		pollCloud();
		// get the game state
//		Intent intent = getIntent();
//		game = (Game)intent.getSerializableExtra("GAME");
		

		// download the category, answer, and hint from the other player
//        game.setAnswer(thecloud.downloadStringMessage(Cloud.MessageType.ANSWER));
//        game.setTip(thecloud.downloadStringMessage(Cloud.MessageType.CLUE));
//        game.setCategory(thecloud.downloadStringMessage(Cloud.MessageType.CATEGORY));
//        theImage = thecloud.downloadImage();
	}
	
	private void pollCloud(){
		
//		final ContextWrapper activity = this;
		final Handler mainHandler = new Handler(this.getMainLooper());
		
		thread = new Thread(new Runnable() {
			
			@Override
			public void run(){
				
				while(!donePoll && !cancel){
					poll();
				
					try{
						Thread.sleep(1000);
					
					}catch (InterruptedException e) {
					}
				}
				
				
			}
			public void poll(){
				Log.i("Status",thecloud.getGameInfoSetting());
				if(thecloud.getGameInfoSetting().equals("drawing")){
					mainHandler.post(new Runnable() {
	            		/*
	            		 * If the user was logged in then we can send them to the next activity
	            		 */
	            		@Override
	    	            public void run() {
	            			otherPlayerFinishedGuess();
	                    }
	            	});
					
				}
			
			}
			
			});
			thread.start();		
	}

	
	//Called when the player is finished with guess
	public void otherPlayerFinishedGuess(){
		view.setText(R.string.waitingingamedrawing);
		donePoll = true;
		
	}
	
	//Called when the other player is all done, so we need to
	//switch over to activities here
	public void otherPlayerFinished(){
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putString( TEXT,view.getText().toString());
		cancel = true;
	}

	
	
	/**
	 * this is used for testing when there is only one player.
	 * continue on to the next activity. send the game state
	 * @param view
	 */
	public void onContinueButtonPressed(View view) {
		Intent intent = new Intent(this, GuessActivity.class);
    	//intent.putExtra("GAME", game);
		startActivity(intent);
    	finish();
	}

}
